/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.hanfu.controller;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.core.result.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.continew.admin.hanfu.domain.bo.CarouselBo;
import top.continew.admin.hanfu.domain.query.CarouselQuery;
import top.continew.admin.hanfu.domain.vo.CarouselVo;
import top.continew.admin.hanfu.service.impl.CarouselServiceImpl;

import java.util.ArrayList;

/**
 * <p>
 * 轮播图前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-03-27
 */
@RestController
@RequestMapping("/carousel")
@RequiredArgsConstructor
public class CarouselController {

    private final CarouselServiceImpl carouselService;

    /**
     * 新增轮播图
     *
     * 轮播图说明：
     * 业务逻辑：
     * 1.因为每张轮播图都有不同的介绍，所以需要前端传入轮播图对应的页面名称，
     * 1.1）这也会导致一个轮播图就仅有一个图片(注：'img' 仅一个)
     *
     * 2.前端要用表单的格式反复发请求，仅能一个一个上传
     *
     * 问题:
     * 1.在sample 表中有一个轮播图id字段,所以这里的轮播图添加后要放入到 sample 表中，
     * 1.1) 因为两个是不同的服务，要么服务新增相应的接口，要么在不同服务中直接调用 mapper(这里规范问题)
     * 2.直接删除 sample表的的轮播图id字段，使用最简单的直接根据 sample id字段来查询出对应的轮播图，
     */

    /**
     * 查询接口
     */
    @PostMapping("condition")
    public R getCarouselListByCondition(@RequestBody CarouselQuery carouselQuery) {

        Page<CarouselVo> carouselListByCondition = carouselService.getCarouselListByCondition(carouselQuery);
        /**
         * 格式化时间格式
         */
        carouselListByCondition.getRecords().forEach(item -> {
            item.setUpdateTime(DateTime.of(Long.parseLong(item.getUpdateTime())).toString("yyyy-MM-dd HH:mm:ss"));
        });
        return R.success(carouselListByCondition);
    }

    /**
     * 查询 by id
     */
    @GetMapping("/{id}")
    public R getCarouselById(@PathVariable("id") Integer id) {
        return R.success(carouselService.getById(id));
    }

    @PostMapping("")
    public R addCarousel(@RequestParam("img") MultipartFile img,
                         @RequestParam("pageName") String pageName,
                         @RequestParam("subject") String subject,
                         @RequestParam("updateBy") String updateBy) {

        if (pageName == null || pageName.equals("")) {
            return R.fail("pageName不能为空");
        }

        CarouselBo carouselBo = new CarouselBo();
        carouselBo.setFile(img);
        carouselBo.setPageName(pageName);
        carouselBo.setSubject(subject);
        carouselBo.setUpdateBy(updateBy);
        int i = carouselService.addCarousel(carouselBo);

        return i == 1 ? R.success() : R.fail("新增轮播图失败");
    }

    /**
     * 删除接口
     */
    @DeleteMapping("")
    public R deleteCarousel(@RequestBody ArrayList<Integer> ids) {
        boolean b = carouselService.removeByIds(ids);
        return b ? R.success() : R.fail("");
    }

    /**
     * 更新接口
     */
    @PutMapping("")
    public R updateCarousel(@RequestParam("img") MultipartFile img,
                            @RequestParam("pageName") String pageName,
                            @RequestParam("subject") String subject,
                            @RequestParam("updateBy") String updateBy,
                            @RequestParam("id") Integer id) {

        CarouselBo carouselBo = new CarouselBo();
        carouselBo.setId(id);
        carouselBo.setSubject(subject);
        carouselBo.setPageName(pageName);
        carouselBo.setUpdateBy(updateBy);
        carouselBo.setFile(img);

        int i = carouselService.updateCarousel(carouselBo);

        return i == 1 ? R.success() : R.fail("");
    }

}
